package com.spboot;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * <p>
 * 测试生成代码
 * </p>
 *
 * @author K神
 * @date 2017/12/18
 */
public class GeneratorServiceEntity {

	public static void main(String[] args) {
		new GeneratorServiceEntity().generateCode();
	}
   
    public void generateCode() {
        String packageName = "com.spboot";
        boolean serviceNameStartWithI = false;//user -> UserService, 设置成true: user -> IUserService
        generateByTables(serviceNameStartWithI, packageName);
    }

    private void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) {
      
	  String dbUrl = "jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=GMT%2B8";
      DataSourceConfig dataSourceConfig = new DataSourceConfig();
      dataSourceConfig.setDbType(DbType.MYSQL)
              .setUrl(dbUrl)
              .setUsername("root")
              .setPassword("123456")
				.setDriverName("com.mysql.cj.jdbc.Driver");
      
    	GlobalConfig config = new GlobalConfig();
        config.setActiveRecord(false)
                .setAuthor("guocb")
                .setOutputDir("E:\\SSM\\spboot")
                .setFileOverride(true)
                .setBaseResultMap(true)//baseResultMap
        		.setBaseColumnList(true);

        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setCapitalMode(true)
                .setEntityLombokModel(true)//实体类Lombok
                .setRestControllerStyle(false)//是否rest风格
                .setEntityColumnConstant(true)//生成列的常量
//                .setControllerMappingHyphenStyle(true)//mapping下划线风格
                .setTablePrefix("smbms_")//去除表名前缀
                .setNaming(NamingStrategy.no_change)//下划线转驼峰
                .setInclude(tableNames);//修改替换成你需要的表名，多个表名传数组
        		
        if (!serviceNameStartWithI) {
            config.setServiceName("%sService");
        }

        PackageConfig pkConfig =   new PackageConfig();
        pkConfig.setParent(packageName)
                .setController("controller")
                .setEntity("entity")
        		.setMapper("mapper")//dao
        		.setService("service")//service
        		.setXml("mapper");//xml
        
        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(pkConfig)
                .execute();
    }

    private void generateByTables(String packageName, String... tableNames) {
        generateByTables(true, packageName, tableNames);
    }
}
